package com.yz.group1.group_1.controller;

import com.yz.group1.group_1.entity.Department;
import com.yz.group1.group_1.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin//允许跨域请求
//接收请求，返回结果
@RestController//标记当前类是一个控制器类  返回结果为JSON数据结构
@RequestMapping("/department")//统一的前缀，前端访问 /department 开头的接口
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @GetMapping
    public List<Department> departmentList(){
        return departmentService.listAllDepartments();
    }
    @PostMapping("/updateStatus")
    public boolean updateStatus(@RequestBody Department department){
        return departmentService.updateById(department);

    }

    //对应的是delete请求，访问“/department/delete”路径是触发api
    //@PathVariable/获取访问路径中的变量
    @DeleteMapping("/delete/{id}")
    public boolean deleteById(@PathVariable Integer id){
        return departmentService.removeById(id);
    }
//    @PostMapping("/delete/{id}")
//    public boolean delete (@PathVariable Integer id ) {
//        return departmentService.removeById(id);
//    }

    //api:application+program+interface 应用程序编程接口
    //对应的是post请求，访问“/department/add”路径是触发api
    @PostMapping("/add")
    public boolean add(@RequestBody Department department){
        return departmentService.save(department);
    }
}
